<template>
  <div>
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="header_box">
        <el-input
          style="width: 168px; margin-right: 24px"
          v-model="query.key.goodsName"
          placeholder="请输入资源名字"
          clearable
        ></el-input>

        <el-button type="primary" @click="clikcUsertListSearch">
          确认搜索
        </el-button>
      </div>

      <el-table :data="initData" style="width: 100%" border>
        <el-table-column label="创建日期" width="180">
          <template slot-scope="scope">
            {{
              $moment(parseInt(scope.row.createdAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column prop="goodsName" label="物品" width="180">
        </el-table-column>

        <el-table-column label="描述图片" width="88px">
          <template slot-scope="scope">
            <img :src="scope.row.describeImgUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="describeText" label="描述"> </el-table-column>
        <el-table-column prop="telephone" label="手机号码" width="180">
        </el-table-column>
        <el-table-column prop="createdBy" label="发布人" width="120">
        </el-table-column>

        <el-table-column label="审核转态" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.checked == 0">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.checked == 1">通过</el-tag>
            <el-tag type="danger" v-if="scope.row.checked == -1">不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="审核" width="160">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.checked"
              @change="changeChecked(scope.row)"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.sta"
                :label="item.name"
                :value="item.sta"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="$router.push('/admin/mutual_aid/add/' + scope.row.id)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="getDeleteGoodsHelp(scope.row.id)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="text-align: center; margin-top: 24px"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleSizeChange"
        :page-size="query.pageSize"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import {
  getDeleteGoodsHelp,
  getGoodsHelpEdit,
  getGoodsHelpList,
} from "@/util/goodsHelp.js";

export default {
  data() {
    return {
      query: {
        key: {
          goodsName: "",
        },
        pageSize: 10,
        pageNum: 1,
      },
      initData: [],
      total: 10,
      options: [
        {
          name: "审核不通过",
          sta: -1,
        },
        {
          name: "待审核",
          sta: 0,
        },
        {
          name: "审核通过",
          sta: 1,
        },
      ],
    };
  },
  created() {
    this.getGoodsHelpList();
  },
  mounted() {},
  methods: {
    // 点击搜索按钮
    clikcUsertListSearch() {
      this.getGoodsHelpList();
    },
    // 获取数据
    async getGoodsHelpList() {
      let { data: res } = await getGoodsHelpList(this.query);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data.list;
      this.total = res.data.total;
    },
    // 删除数据
    async getDeleteGoodsHelp(id) {
      let { data: res } = await getDeleteGoodsHelp({ id: id });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.getGoodsHelpList();
      console.log(res);
    },
    // 修改审核转态
    async changeChecked(e) {
      console.log(e);
      let { data: res } = await getGoodsHelpEdit({
        id: e.id,
        checked: e.checked,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.getGoodsHelpList();
    },
    // 分页
    handleSizeChange(e) {
      this.query.pageNum = e;
      this.getGoodsHelpList();
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  /deep/.cell {
    max-height: 50px;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    img {
      height: 50px;
    }
  }
}
</style>
